﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Views;

namespace Presenters
{
    /// <summary>
    /// Presenter for the Save Items view
    /// </summary>
    public class SaveItemsPresenter : PresenterBase<ISaveItemsView>
    {
        #region Public

        /// <summary>
        /// Initialise the Save Items presenter
        /// </summary>
        /// <param name="view">An instance that impliments ISaveItems</param>
        /// <param name="queryViewsToSave">A list of possible query views to save</param>
        public SaveItemsPresenter(ISaveItemsView view, List<IQueryView> queryViewsToSave)
            : base(view)
        {
            // Get a list of dirty query views out of the supplied list and assign it to the View
            View.QueryViewsToSave = queryViewsToSave.Where(queryView => queryView.IsDirty).ToList();

            // Select all queries for saving
            foreach (IQueryView queryView in View.QueryViewsToSave)
            {
                queryView.SelectedForSaving = true;
            }
        }

        /// <summary>
        /// Set the list of items that we want to save
        /// </summary>
        public void SaveItems()
        {
            // Set the list of items that we want to save
            View.QueriesSelectedToSave = View.QueryViewsToSave.Where(queryView => queryView.SelectedForSaving).ToList();

            // If the list of queries to save is NULL (non were selected) then set the list to an empty one
            if (View.QueriesSelectedToSave == null)
            {
                View.QueriesSelectedToSave = new List<IQueryView>();
            }
        }

        /// <summary>
        /// Set the list of items we want to save to NULL which will cancel the save process
        /// </summary>
        public void CancelSave()
        {
            View.QueriesSelectedToSave = null;
        }

        /// <summary>
        /// Set the list of selected queries to save to an empty list so none will be saved
        /// </summary>
        public void SaveNone()
        {
            View.QueriesSelectedToSave = new List<IQueryView>();
        }

        #endregion Public

    }
}
